/*
 * Created by SharpDevelop.
 * User: phtrung
 * Date: 5/5/2010
 * Time: 10:20 AM
 * 
 * To change this template use Tools | Options | Coding | Edit Standard Headers.
 */

using System;
using System.ComponentModel;
using System.Drawing;
using System.Windows.Forms;

namespace popLib.CustomeControl
{
	/// <summary>
	/// Description of OptionPanel.
	/// </summary>
	public partial class OptionPanel : UserControl
	{
		OptionPanelItem[] panelContainer = new OptionPanelItem[0];
        Label[] labels = new Label[0];
        public FlowLayoutPanel ButtonTabContainer { get { return flpl_buttonContainer; } }

        private bool _buttonTabReadOnly;
        public bool ButtonTabReadOnly { 
            get { 
                return _buttonTabReadOnly; 
            }
            set
            {
                _buttonTabReadOnly = value;
            }
        }

		public OptionPanel()
		{
			//
			// The InitializeComponent() call is required for Windows Forms designer support.
			//
			InitializeComponent();						
		}
		
		public OptionPanelItem[] Panel{
			get{
				return panelContainer;
			}
			set{
				panelContainer = value;				
				initInterface();
			}
		}

        public int SelectedTabIndex {
            get { return NumberLib.getInt(flpl_buttonContainer.Text); }
            set {
                if (panelContainer.Length > 0)
                {
                    if (value < panelContainer.Length && value >= 0)
                        viewPanel(labels[value]);
                    else if( value >= panelContainer.Length)
                        viewPanel(labels[panelContainer.Length - 1]);
                    else if ( value < 0)
                        viewPanel(labels[0]);
                }
            }
        }
                		
		void initInterface(){
			int counter = 0;
			pnl_container.Controls.Clear();
			flpl_buttonContainer.Controls.Clear();
            labels = new Label[panelContainer.Length];
			
			foreach( OptionPanelItem panel in panelContainer ){					
				panel.Dock = DockStyle.Fill;
				panel.Tag = counter;
				panel.BackColor = Color.White;
				pnl_container.Controls.Add(panel);
				
				Label label = new Label();
				label.Text = "  "+panel.PanelTitle;				
				label.Size = new Size( 190, 30 );
				label.ForeColor = Color.FromArgb(0,21,110);
				label.Tag = counter;
				label.TextAlign = ContentAlignment.MiddleLeft;
				label.Margin = new Padding( 3 );
				
				if( counter == 0 ){
					label.BackgroundImage = (Bitmap)PopLibRes.button_selected;
					flpl_buttonContainer.Text = "0";
				}

                label.MouseEnter += new EventHandler(label_MouseEnter);
                label.MouseLeave += new EventHandler(label_MouseLeave);
                label.Click += new EventHandler(label_Click);
				
				flpl_buttonContainer.Controls.Add( label );
                labels[counter] = label;

				counter++;
			}
		}

        void label_Click(object sender, EventArgs e)
        {
            if (ButtonTabReadOnly) return;
            viewPanel((Label)sender);
        }

        void label_MouseLeave(object sender, EventArgs e)
        {
            if (ButtonTabReadOnly) return;

            if (flpl_buttonContainer.Text != ((Label)sender).Tag.ToString())
                ((Label)sender).BackgroundImage = null;
        }

        void label_MouseEnter(object sender, EventArgs e)
        {
            if (ButtonTabReadOnly) return;

            if (flpl_buttonContainer.Text != ((Label)sender).Tag.ToString())
                ((Label)sender).BackgroundImage = (Bitmap)PopLibRes.button_over;
        }
		
		void viewPanel( Label label ){
			foreach( OptionPanelItem panel in panelContainer ){
				if( panel.Tag.Equals(label.Tag) ){
					//panel.Show();
					panel.BringToFront();
				}else{
					//panel.Hide();
				}
			}
			
			foreach( Label eachLabel in flpl_buttonContainer.Controls ){				
				eachLabel.BackgroundImage = null;				
			}

            label.BackgroundImage = (Bitmap)PopLibRes.button_selected;			
			flpl_buttonContainer.Text = label.Tag.ToString();
		}

        private void OptionPanel_Load(object sender, EventArgs e)
        {
            initInterface();
        }

       
	}
	
	public class OptionPanelItem: Panel{
		string panelCommand = "Button Command";
		public string PanelTitle{
			get{ return panelCommand; }
			set{ panelCommand = value; }
		}
	}
}
